//
//  TTImageDingzhiUploadViewCell.m
//  TaoTuo
//
//  Created by 刘广 on 2020/3/27.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTImageDingzhiUploadViewCell.h"
#import <UIImageView+WebCache.h>

@interface TTImageDingzhiUploadViewCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation TTImageDingzhiUploadViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, self.width - 10, self.height - 10)];
        _imageView.backgroundColor = UIColorHex(f2f2f2);
//        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_imageView];
        _imageView.layer.cornerRadius = 5;
        _imageView.layer.masksToBounds = YES;
        
        _timeLabel = [UILabel labelWithTitle:@"" color:[UIColor whiteColor] fontSize:12];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.frame = CGRectMake(0, self.bottom - 30, self.width - 15, 30);
        [self.contentView addSubview:_timeLabel];
        
        _deleteBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"reddelete"] target:self action:@selector(deleteBtnClick)];
        _deleteBtn.frame = CGRectMake(self.imageView.width - 10, 0, 20, 20);
        _deleteBtn.imageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_deleteBtn];
        
        
    }
    return self;
}

- (void)deleteBtnClick
{
    if (self.deleteBlock) {
        self.deleteBlock(self.model);
    }
}

- (void)setModel:(TTImageDingzhiModel *)model
{
    _model = model;
    
    if (model.imageName.length > 0) {
        self.imageView.image = [UIImage imageNamed:model.imageName];
    } else {
        if (model.urlString.length > 0) {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.urlString] placeholderImage:nil options:SDWebImageRetryFailed];
        }
    }
    
    if (model.thumbImage) {
        self.imageView.image = model.thumbImage;
    }
    
    if (model.videoTime > 0) {
        self.timeLabel.text = [NSString stringWithFormat:@"%.0fs", model.videoTime];
    }
    
    self.timeLabel.hidden = model.isImage;
    self.deleteBtn.hidden = model.isVideo && model.imageName.length > 0;
}

@end
